package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "地址相关接口")
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @ApiOperation("查询所有地址")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询地址开始");
        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList);
    }

    /**
     * 获取当前用户的默认地址
     */
    @ApiOperation("获取默认地址")
    @GetMapping("/default")
    public Result<AddressBook> defaultAddress(){
        log.info("查询用户默认地址开始");
        AddressBook addressBook = addressBookService.defaultAddress();
        return Result.success(addressBook);
    }

    /**
     * 通过地址ID获取当前地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("地址ID获取地址")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id){
        log.info("当前的地址ID：{}",id);
        AddressBook addressBook = addressBookService.getAddressBookById(id);
        return Result.success(addressBook);
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result saveAddressBook(@RequestBody AddressBook addressBook){
        log.info("新增地址的信息:{}",addressBook);
        addressBookService.saveAddress(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址的ID：{}",addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据ID修改地址信息
     * @param addressBook
     * @return
     */
    @ApiOperation("根据ID修改地址")
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
        log.info("修改后的地址信息：{}",addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    /**
     * 根据ID删除地址
     * @param id
     * @return
     */
    @ApiOperation("根据ID删除地址")
    @DeleteMapping
    public Result deleteAddressBook(Long id){
        log.info("删除当前id的地址：{}",id);
        addressBookService.deleteAddressById(id);
        return Result.success();
    }

}
